﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using DeadDog;
using SC2parser.UserControls;
using SC2parser.Model;
using SC2parser.Function;

namespace SC2parser.Parsing
{
    class GroupStageParser
    {
        public GroupStageParser()
        {
            //LiquipediaPage = new URL(liquipediaPage);
        }

        //private URL liquipediaPage;
        //public URL LiquipediaPage
        //{ 
        //    get {return liquipediaPage; }
        //    set {liquipediaPage = value; }
        //}

        private string[] GetSourceGroups(URL page)
        {
            SCBuffer buffer = new SCBuffer("GSLBuffer.xml");
            string source = buffer.ReadURL(page.Address);
            source = source.CutToFirst("==== Group", CutDirection.Left, false);
            source = source.CutToFirst("{{Box|end}}", CutDirection.Right, false);
            //source = source.CutToFirst("|", CutDirection.Left, false);
            buffer.Save("GSLBuffer.xml");

            return source.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        }

        private string name1, race1, score1, name2, race2, score2, flag;
        private int points1 = 0, points2 = 0, bestOf = 0;
        private List<Match> GetMatches(string[] source)
        {
            List<PlayerView> Players = new List<PlayerView>();
            List<Match> matches = new List<Match>();

            bool finishedGettingMatches = false;

            int matchId = 1;
            foreach (string line in source)
            {
                if (line.Contains("player1"))
                {
                    name1 = line.CutToSection("=", "|", true);
                    race1 = line.CutToFirst("race=", CutDirection.Left, true);
                    score1 = "1";
                    //flag1 = line.CutToFirst("flag=", CutDirection.Left, true);
                }
                if (line.Contains("player2"))
                {
                    name2 = line.CutToSection("=", "|", true);
                    race2 = line.CutToFirst("race=", CutDirection.Left, true);
                    score2 = "2";
                }
                if (line.Contains("map1win"))
                {
                    string temp = line.CutToSection("win=", "|", true);
                    if (score1 == temp)
                    {
                        points1++;
                    }
                    if (score2 == temp)
                    {
                        points2++;
                    }
                    else bestOf++;
                }
                if (line.Contains("map2win"))
                {
                    string temp = line.CutToSection("win=", "|", true);
                    if (score1 == temp)
                    {
                        points1++;
                    }
                    if (score2 == temp)
                    {
                        points2++;
                    }
                    else bestOf++;
                }
                if (line.Contains("map3win"))
                {
                    string temp = line.CutToSection("win=", "|", true);
                    if (score1 == temp)
                    {
                        points1++;
                    }
                    if (score2 == temp)
                    {
                        points2++;
                    }
                    else bestOf++;
                }


                if (points1 >= 2 || points2 >= 2)
                {
                    matches.Add(new Match(new Player(name1, race1, points1), new Player(name2, race2, points2), matchId));
                    points1 = 0;
                    points2 = 0;
                }
                else if (bestOf >= 3)
                {
                    matches.Add(new Match(new Player(name1, race1, points1), new Player(name2, race2, points2), matchId));
                    bestOf = 0;
                }
                matchId++;


                if (line == "{{Box|end}}")
                {
                    finishedGettingMatches = true;
                }
            }

            if (finishedGettingMatches)
            {
                foreach (string line in source)
                {
                    if (line.Contains("GroupTableSlot"))
                    {
                        flag = line.CutToSection("flag=", "|", true);
                        
                        foreach (Match m in matches)
                        {
                            string playerOne = line.CutToSection("race=" + m.PlayerOne.Race.raceToString() + "|", "}}", true);
                            if (playerOne == m.PlayerOne.Name)
                            {
                                m.PlayerOne.Country = flag;
                            }
                            string playerTwo = line.CutToSection("race=" + m.PlayerTwo.Race.raceToString() + "|", "}}", true);
                            if (playerTwo == m.PlayerTwo.Name)
                            {
                                m.PlayerTwo.Country = flag;
                            }
                        }
                    }
                }
            }

            return matches;
        }

        public List<Match> ParseGrupStage(string liquipediaPage)
        {
            return GetMatches(GetSourceGroups(new URL(liquipediaPage)));
        }
    }
}
